import 'package:dio/dio.dart';
import 'package:flutter_changdian_football/api/api.dart';
import 'package:flutter_changdian_football/api/api_response.dart';
import 'package:flutter_changdian_football/api/http_utils.dart';
import 'package:flutter_changdian_football/global.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';

class ReqClub {

  /// 俱乐部 - 俱乐部切换 - 已加入的俱乐部列表
  static Future<ApiResponse<dynamic>> getJoinClubList() async {
    try {
      var response = await HttpUtils.get(Api.getJoinClubList);
      return ApiResponse.completed(response);
    } on DioError catch (e) {
      return ApiResponse.error(e.error);
    } finally {
      EasyLoading.dismiss();
    }
  }

  /// 俱乐部主页
  static Future<ApiResponse<dynamic>> clubCenter({
    required String clubId
  }) async {
    try {
      var response = await HttpUtils.post(Api.clubCenter, data: {
        'clubId': clubId
      });
      return ApiResponse.completed(response);
    } on DioError catch (e) {
      return ApiResponse.error(e.error);
    } finally {
      EasyLoading.dismiss();
    }
  }

  /// 俱乐部动态信息分页查询
  static Future<ApiResponse<dynamic>> clubDynamic({
    required String clubId,
    required String pageNum,
    required String pageSize,
  }) async {
    try {
      var response = await HttpUtils.post(Api.clubDynamic, data: {
        'clubId': clubId,
        'pageNum': pageNum,
        'pageSize': pageSize
      });
      return ApiResponse.completed(response);
    } on DioError catch (e) {
      return ApiResponse.error(e.error);
    } finally {
      EasyLoading.dismiss();
    }
  }

  /// 俱乐部成员列表
  static Future<ApiResponse<dynamic>> clubMemberList(
    {
      required String clubId,
      int pageNum = 1
    }
  ) async {
    try {
      var response = await HttpUtils.post(Api.clubMemberList, data: {
        'clubId': clubId,
        'pageNum': pageNum,
        'pageSize': Global.pageSize
      });
      return ApiResponse.completed(response);
    } on DioError catch (e) {
      return ApiResponse.error(e.error);
    } finally {
      EasyLoading.dismiss();
    }
  }

  /// 俱乐部-添加无故缺席标记
  static Future<ApiResponse<dynamic>> absentMark({
    required String clubId,
    required String markedUserId,
    required String matchId,
    required String punishmentRuleCode,
  }) async {
    try {
      var response = await HttpUtils.post(Api.absentMark, data: {
        "clubId": clubId,
        "markedUserId": markedUserId,
        "matchId": matchId,
        "punishmentRuleCode": punishmentRuleCode
      });
      return ApiResponse.completed(response);
    } on DioError catch (e) {
      return ApiResponse.error(e.error);
    } finally {
      EasyLoading.dismiss();
    }
  }

  /// 俱乐部成员个人详情
  static Future<ApiResponse<dynamic>> clubMemberDetail({
    required String queriedUserId,
    required String clubId
  }) async {
    try {
      var response = await HttpUtils.post(Api.clubMemberDetail, data: {
        "queriedUserId": queriedUserId,
        'clubId': clubId
      });
      return ApiResponse.completed(response);
    } on DioError catch (e) {
      return ApiResponse.error(e.error);
    } finally {
      EasyLoading.dismiss();
    }
  }

  /// 俱乐部球员列表-成员管理
  static Future<ApiResponse<dynamic>> clubMemberAppoint({
    required String appointType,
    required String appointedUserId,
    required String clubId
  }) async {
    try {
      var response = await HttpUtils.post(Api.clubMemberAppoint, data: {
        "appointType": appointType,
        "appointedUserId": appointedUserId,
        "clubId": clubId
      });
      return ApiResponse.completed(response);
    } on DioError catch (e) {
      return ApiResponse.error(e.error);
    } finally {
      EasyLoading.dismiss();
    }
  }

  /// 俱乐部财政当前余额
  static Future<ApiResponse<dynamic>> currentBalance({
    required String clubId,
  }) async {
    try {
      var response = await HttpUtils.post(Api.currentBalance, data: {
        "clubId": clubId,
      });
      return ApiResponse.completed(response);
    } on DioError catch (e) {
      return ApiResponse.error(e.error);
    } finally {
      EasyLoading.dismiss();
    }
  }

  /// 俱乐部财政 - 资金记录
  static Future<ApiResponse<dynamic>> financesRecords(
    {
      required String clubId,
      int pageNum = 1,
      String? startTime,
      String? endTime 
    }
  ) async {
    try {
      var response = await HttpUtils.post(Api.financesRecords, data: {
        "clubId": clubId,
        "endTime": endTime,
        "pageNum": pageNum,
        "pageSize": Global.pageSize,
        "startTime": startTime
      });
      return ApiResponse.completed(response);
    } on DioError catch (e) {
      return ApiResponse.error(e.error);
    } finally {
      EasyLoading.dismiss();
    }
  }

  /// 俱乐部详情
  static Future<ApiResponse<dynamic>> clubDetail({
    required String clubId,
  }) async {
    try {
      var response = await HttpUtils.post(Api.clubDetail, data: {
        "clubId": clubId,
      });
      return ApiResponse.completed(response);
    } on DioError catch (e) {
      return ApiResponse.error(e.error);
    } finally {
      EasyLoading.dismiss();
    }
  }

  /// 修改俱乐部
  static Future<ApiResponse<dynamic>> updateClub({
    required String clubId,
    String? clubAddress,
    String? clubHomeCourtId,
    String? clubHomeCourtName,
    String? clubImage,
    String? clubName,
    String? clubProfile,
    String? clubSportsName,
    String? clubSportsValue,
    String? clubRegionCode
  }) async {
    try {
      var response = await HttpUtils.post(Api.updateClub, data: {
        "clubId": clubId,
        'clubAddress': clubAddress,
        'clubHomeCourtId': clubHomeCourtId ?? 0,
        'clubHomeCourtName': clubHomeCourtName,
        'clubImage': clubImage,
        'clubName': clubName,
        'clubProfile': clubProfile,
        'clubSportsName': clubSportsName,
        'clubSportsValue': clubSportsValue,
        'clubRegionCode': clubRegionCode
      });
      return ApiResponse.completed(response);
    } on DioError catch (e) {
      return ApiResponse.error(e.error);
    } finally {
      EasyLoading.dismiss();
    }
  }

  /// 俱乐部消息审核分页
  static Future<ApiResponse<dynamic>> checkMessages(
    {
      required String clubId,
      int pageNum = 1,
    }
  ) async {
    try {
      var response = await HttpUtils.post(Api.checkMessages, data: {
        "clubId": clubId,
        "pageNum": pageNum,
        "pageSize": Global.pageSize,
      });
      return ApiResponse.completed(response);
    } on DioError catch (e) {
      return ApiResponse.error(e.error);
    } finally {
      EasyLoading.dismiss();
    }
  }

  /// 加入俱乐部申请审核 - 拒绝
  static Future<ApiResponse<dynamic>> checkReject({
    required String clubId,
    required String userId,
    required String messageId
  }) async {
    try {
      var response = await HttpUtils.post(Api.checkReject, data: {
        "clubId": clubId,
        "messageId": messageId,
        "userId": userId
      });
      return ApiResponse.completed(response);
    } on DioError catch (e) {
      return ApiResponse.error(e.error);
    } finally {
      EasyLoading.dismiss();
    }
  }

  /// 加入俱乐部申请审核 - 通过
  static Future<ApiResponse<dynamic>> checkPass({
    required String clubId,
    required String userId,
    required String messageId
  }) async {
    try {
      var response = await HttpUtils.post(Api.checkPass, data: {
        "clubId": clubId,
        "messageId": messageId,
        "userId": userId
      });
      return ApiResponse.completed(response);
    } on DioError catch (e) {
      return ApiResponse.error(e.error);
    } finally {
      EasyLoading.dismiss();
    }
  }

  /// 设置俱乐部公告栏
  static Future<ApiResponse<dynamic>> setClubBulletin({
    required String clubId,
    required String bulletin
  }) async {
    try {
      var response = await HttpUtils.post(Api.setClubBulletin, data: {
        "clubId": clubId,
        "bulletin": bulletin,
      });
      return ApiResponse.completed(response);
    } on DioError catch (e) {
      return ApiResponse.error(e.error);
    } finally {
      EasyLoading.dismiss();
    }
  }

  /// 新增俱乐部
  static Future<ApiResponse<dynamic>> addClub({
    required String clubAddress,
    required String clubHomeCourtId,
    required String clubHomeCourtName,
    required String clubImage,
    required String clubName,
    required String clubProfile,
    required String clubRegionCode,
    required String clubSportsValue
  }) async {
    try {
      var response = await HttpUtils.post(Api.addClub, data: {
        "clubAddress": clubAddress,
        "clubHomeCourtId": clubHomeCourtId,
        "clubHomeCourtName": clubHomeCourtName,
        "clubImage": clubImage,
        "clubName": clubName,
        "clubProfile": clubProfile,
        "clubRegionCode": clubRegionCode,
        "clubSportsValue": clubSportsValue
      });
      return ApiResponse.completed(response);
    } on DioError catch (e) {
      return ApiResponse.error(e.error);
    } finally {
      EasyLoading.dismiss();
    }
  }

  /// 俱乐部-用户俱乐部显示名回显接口
  static Future<ApiResponse<dynamic>> displayName({
    required String clubId,
  }) async {
    try {
      var response = await HttpUtils.post(Api.displayName, data: {
        "clubId": clubId,
      });
      return ApiResponse.completed(response);
    } on DioError catch (e) {
      return ApiResponse.error(e.error);
    } finally {
      EasyLoading.dismiss();
    }
  }

  /// 俱乐部-设置用户的俱乐部内显示名称
  static Future<ApiResponse<dynamic>> setPlayerClubDisplayName({
    required String clubId,
    required String setName
  }) async {
    try {
      var response = await HttpUtils.post(Api.setPlayerClubDisplayName, data: {
        "clubId": clubId,
        "setName": setName
      });
      return ApiResponse.completed(response);
    } on DioError catch (e) {
      return ApiResponse.error(e.error);
    } finally {
      EasyLoading.dismiss();
    }
  }

  /// 俱乐部排行
  static Future<ApiResponse<dynamic>> clubRank({
    String? keyword,
    int pageNum = 1,
    String? pcode,
    String orderType = 'ACTIVITY'
  }) async {
    try {
      var response = await HttpUtils.post(Api.clubRank, data: {
        "keyword": keyword,
        "pageNum": pageNum,
        "pageSize": Global.pageSize,
        "pcode": pcode,
        "orderType": orderType
      });
      return ApiResponse.completed(response);
    } on DioError catch (e) {
      return ApiResponse.error(e.error);
    } finally {
      EasyLoading.dismiss();
    }
  }

  /// 申请加入俱乐部
  static Future<ApiResponse<dynamic>> joinApply({
    required String clubId,
    required String message
  }) async {
    try {
      var response = await HttpUtils.post(Api.joinApply, data: {
        "clubId": clubId,
        "message": message
      });
      return ApiResponse.completed(response);
    } on DioError catch (e) {
      return ApiResponse.error(e.error);
    } finally {
      EasyLoading.dismiss();
    }
  }

  /// 切换俱乐部
  static Future<ApiResponse<dynamic>> toggleDefaultClub({
    required String clubId,
  }) async {
    try {
      var response = await HttpUtils.post(Api.toggleDefaultClub, data: {
        "clubId": clubId,
      });
      return ApiResponse.completed(response);
    } on DioError catch (e) {
      return ApiResponse.error(e.error);
    } finally {
      EasyLoading.dismiss();
    }
  }

  /// 回显俱乐部公告栏内容
  static Future<ApiResponse<dynamic>> getClubBulletin({
    required String clubId,
  }) async {
    try {
      var response = await HttpUtils.post(Api.getClubBulletin, data: {
        "clubId": clubId,
      });
      return ApiResponse.completed(response);
    } on DioError catch (e) {
      return ApiResponse.error(e.error);
    } finally {
      EasyLoading.dismiss();
    }
  }

}